//富文本解析器
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 动画
    animationData: {},
    status:true,



    // 音乐
    music:'',
    title:'',
    image:'',

    value:0,
    maxvalue:''
  },
  kj:function(){
    wx.showToast({
      title: '快进中...',
      icon:'none',
      duration:1000
    })
    let that = this
    let time = that.data.value+10
    console.log(that.data.maxvalue)
    
    this.setData({
      value:time
    })
    console.log(time)
    wx.seekBackgroundAudio({
      position: time
    })

    
  },
  kt:function(){
    wx.showToast({
      title: '快退中...',
      icon:'none',
      duration:1000
    })
    let time = this.data.value-10
    if(time <= 0){
      time=0
    }
    
    this.setData({
      value:time
    })
    console.log(time)
    wx.seekBackgroundAudio({
      position: time
    })
  },


  stop:function(){
    this.setData({
      status:!this.data.status
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    // console.log(options)
    // that.setData({
    //   index:options.index
    // })
    wx.getStorage({
      key: 'tmusic',
      success: function(res) {
        console.log(res)
        that.setData({
          music:res.data.music,
          title:res.data.name,
          image:res.data.image,
        })
        if(res.data.desrc){
          console.log("desrc")
          WxParse.wxParse('article', 'html', res.data.desrc, that, 5);
          that.setData({
            desrc:res.data.desrc
          })
        }
        that.music()
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  // 开始播放
  audioPlay(e){

    let that = this
    if(!that.data.music){
//    wx.showToast({
//      title:'音频地址不正确',
//      icon:'none',
//      duration:2000
//    })
      that.setData({
        status:false
      })
      return false
    }

    that.setData({
      status:true
    })
    that.backmusic.play()

    // if(that.data.value > 1){
    //   wx.playBackgroundAudio({
    //     dataUrl: that.data.music,
    //     title: that.data.title,
    //     coverImgUrl: that.data.image
    //   })
    // }
    // that.music()
    // let gotime = that.data.value
    //   console.log(gotime)
    //   wx.seekBackgroundAudio({
    //     position: 30
    //   })



    that.backmusic.onTimeUpdate(()=>{
      //console.log(that.backmusic.duration)  //总时长
      //console.log(that.backmusic.currentTime)//进行
        // 计算时长
        // let sumtime = that.backmusic.duration;
        // let newtime = that.backmusic.currentTime
        // let width = newtime / sumtime * 100 + '%'
        // 总时间
        // let sumtimem = parseInt(sumtime / 60);
        // let sumtimes = parseInt(sumtime % 60);
        // if (sumtimem < 10) {
        //   sumtimem = "0" + sumtimem
        // }
        // if (sumtimes < 10) {
        //   sumtimes = "0" + sumtimes
        // }
        // let newtimem = parseInt(newtime / 60);
        // let newtimes = parseInt(newtime % 60);


        // if (newtimem < 10) {
        //   newtimem = "0" + newtimem
        // }
        // if (newtimes < 10) {
        //   newtimes = "0" + newtimes
        // }

        // let end = sumtimem + ':' + sumtimes;
        // let start = newtimem + ':' + newtimes
        // console.log(end)
        // console.log(start)

      that.setData({
        value:that.backmusic.currentTime,
        maxvalue:that.backmusic.duration,
        // starttime:start,
        // endtime:end
      })
    })

    // 循环播放
    that.backmusic.onEnded(function(){
      that.music()
    })

  },
  // 停止播放
  audioPause(){
    let that = this
    that.setData({
      status:false
    })
    that.backmusic.pause()
  },



  // 创建背景音乐
  music(){
    let that = this
    that.backmusic = wx.getBackgroundAudioManager()
    that.backmusic.src =  that.data.music
    that.backmusic.title = that.data.title
    
  },

 
})